package com.uchengwang.ui.cm_ui.umsg.ui;

import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.ClubConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.ucity.adapter.ChooseSchoolAdapter;
import com.uchengwang.ui.cm_ui.umsg.bean.CLubChooseListBean;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.UCommonUtil;


/**
 * Created by U城网 on 2017/9/12.
 * 获取本校社团
 */

public class ClubsActivity extends BaseWithTitleActivity{
    private RecyclerView recycleView;
    private PtrClassicFrameLayout mSwipeRefreshLayout;
    private RecyclerAdapterWithHF mAdapter;
    private ChooseSchoolAdapter adapter;
    private int page, totalPages;

    private long uniId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_ustudy_2);
        uniId = getIntent().getLongExtra(BaseConfig.UNI_ID, -1);

        setTitle("本校社团");
        initView();


    }

    private void initView(){
        recycleView = (RecyclerView) findViewById(R.id.u_study_recycleView);
        mSwipeRefreshLayout = (PtrClassicFrameLayout) findViewById(R.id.mRefreshLayout);

        initData();

    }

    private void initData(){
        adapter = new ChooseSchoolAdapter(this, 2);

        mAdapter = new RecyclerAdapterWithHF(adapter);
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.addItemDecoration(new DividerItemDecoration(ClubsActivity.this, DividerItemDecoration.VERTICAL));
        recycleView.setAdapter(mAdapter);

        mSwipeRefreshLayout.setAutoLoadMoreEnable(true);
        mSwipeRefreshLayout.setLoadMoreEnable(true);

        mSwipeRefreshLayout.postDelayed(new Runnable() {

            @Override
            public void run() {
                mSwipeRefreshLayout.autoRefresh(true);
            }
        }, 150);

        mSwipeRefreshLayout.setPtrHandler(new PtrDefaultHandler() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;
                mSwipeRefreshLayout.loadMoreComplete(true);
                getClubs(true);
            }
        });

        mSwipeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void loadMore() {
                page++;
                if (page <= totalPages){
                    //当前请求页大于总页数，不再请求
                    getClubs(false);
                } else {
                    mSwipeRefreshLayout.loadMoreComplete(false);
                }
            }
        });

    }

    private void getClubs(final boolean isRefresh){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.PAGE, page);
        map.put(BaseConfig.PERPAGE, 20);

        String url = "";
        if (uniId >0){
            url = ClubConfig.getSchoolClubs.URL+"/"+uniId;
        }else {
            url = ClubConfig.getSchoolClubs.URL+"/"+UChengApplication.myUniId;
        }
        HttpManager.newInstance(this).query(ClubConfig.getSchoolClubs.method, url, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjg", "stutas="+status+"=msg="+msg);
                if (isRefresh){
                    mSwipeRefreshLayout.refreshComplete();
                }else {
                    mSwipeRefreshLayout.loadMoreComplete(true);
                }

                UCommonUtil.getUtil().showToast(ClubsActivity.this, "获取失败");

            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjg", "stutas="+status+"=msg="+msg);
                if (isRefresh){
                    mSwipeRefreshLayout.refreshComplete();
                }else {
                    mSwipeRefreshLayout.loadMoreComplete(true);
                }
                if (status != 0){
                    return;
                }

                if (!TextUtils.isEmpty(data) && !data.equals("[]")){
                    CLubChooseListBean bean = JsonUtils.getManager().stringToBean(data, CLubChooseListBean.class);
                    totalPages = bean.getTotalPage();

                    if (isRefresh){
                        adapter.setClubData(bean.getData());
                    }else {
                        adapter.getClubData().addAll(bean.getData());
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        });


    }

}
